<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 苗木种类管理
 */
class TypeController extends BaseController {

    /**
     * 种类列表
     */
    public function typeList() {
        //获取所有苗木种类
        $allTypeData = M('TreeType')->field("id,name,pid,sort,path,CONCAT(path,',',id) AS paths")
                ->where('status=1')
                ->order("paths ASC")
                ->select();

        if (!empty($allTypeData)) {
            foreach ($allTypeData as &$item) {
                $cnt = count(explode(',', $item['path']));
                $item['space'] = str_repeat('--', $cnt);
            }
            unset($item);
        }
        $this->assign('allTypeData', $allTypeData);
        $this->display();
    }

    /**
     * 添加种类
     */
    public function addType() {
        $name = I('post.name', '');
        $pid = I('post.pid', 0);
        $treeType = M('TreeType');

        if (empty($name)) {
            ajaxReturn('种类名称不能为空', 300);
        }
        $existName = $treeType->where("name='{$name}'")->count();
        if ($existName) {
            ajaxReturn('该种类名称已存在', 300);
        }

        $data = $treeType->create();

        if ($data) {
            if (!empty($pid)) {
                $parent = $treeType->field('id,path')->where("id={$pid}")->find();
                $data['path'] = $parent['path'] . ',' . $pid;
            } else {
                $data['path'] = '0';
            }

            $ret = $treeType->add($data);

            if ($ret) {
                ajaxReturn('添加种类成功', 200);
            } else {
                ajaxReturn('添加种类失败', 300);
            }
        } else {
            ajaxReturn('创建数据对象失败', 300);
        }
    }

    /**
     * 修改种类
     */
    public function updType() {
        $typeId = I('post.typeId', 0);
        $name = I('post.name', '');
        $pid = I('post.pid', 0);
        $treeType = M('TreeType');

        if (empty($typeId)) {
            ajaxReturn('种类ID不能为空', 300);
        }
        $typeInfo = $treeType->where("id={$typeId}")->find();
        if (empty($typeInfo)) {
            ajaxReturn('没有找到该种类信息', 300);
        }
        if (empty($name)) {
            ajaxReturn('种类名称不能为空', 300);
        }
        $existName = $treeType->where("name='{$name}' AND id!={$typeId}")->count();
        if ($existName) {
            ajaxReturn('该种类名称已存在', 300);
        }
        if (!empty($pid)) {
            if ($typeId == $pid) {
                ajaxReturn('父级种类不能是自已', 300);
            }
        }

        $data = $treeType->create();

        if ($data) {
            if (!empty($pid)) {
                $parent = $treeType->field('path')->where("id={$pid}")->find();
                $data['path'] = $parent['path'] . ',' . $pid;
            } else {
                $data['path'] = '0';
            }

            $ret = $treeType->where("id={$typeId}")->save($data);

            if ($ret !== false) {
                if ($typeInfo['pid'] != $pid) {
                    $this->reGenChildPath($typeId);
                }

                ajaxReturn('修改种类成功', 200);
            } else {
                ajaxReturn('修改种类失败', 300);
            }
        } else {
            ajaxReturn('创建数据对象失败', 300);
        }
    }

    /**
     * 重新生成孩子路径
     */
    public function reGenChildPath($pid) {
        $treeType = M('TreeType');
        $childs = $treeType->where("pid={$pid}")->select();
        if (empty($childs)) {
            return;
        }
        $parent = $treeType->where("id={$pid}")->find();
        foreach ($childs as $child) {
            $treeType->where("id={$child['id']}")->save(array('path' => $parent['path'] . ',' . $parent['id']));
            $this->reGenChildPath($child['id']);
        }
    }

    /**
     * 删除种类
     */
    public function delType() {
        $typeId = I('post.typeId', 0);

        if (empty($typeId)) {
            ajaxReturn('种类ID不能为空', 300);
        }

        $hasTree = M('Tree')->where("type_id={$typeId}")->count();
        if ($hasTree) {
            ajaxReturn('该种类下有苗木,不能删除', 300);
        }

        $ret = M('TreeType')->where("id={$typeId}")->delete();
        if ($ret !== false) {
            ajaxReturn('删除种类成功', 200);
        } else {
            ajaxReturn('删除种类失败', 300);
        }
    }

    /**
     * 获取子种类信息
     */
    public function getChildType() {
        $typeId = I('post.typeId', 0);

        if (empty($typeId)) {
            ajaxReturn('', 300, array());
        }

        $list = M('TreeType')->field('id,name,pid')->where("status=1 AND pid={$typeId}")->order('sort ASC')->select();
        if (empty($list)) {
            ajaxReturn('', 300, array());
        }
        ajaxReturn('', 200, array('list' => $list));
    }

}
